﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace SimpleCompiler
{
    public enum ControlFlowFlag
    {
        Next,
        For_Continue,
        For_Break,
        Return,
    }

    [DebuggerDisplay("{value.ValueString()}")]
    public class Variable
    {        
        public static Variable None = new Variable();


        public string name = "";        
        public TypeNotNone value { get; set; } = new TypeNotNone();        
        public ControlFlowFlag flag = ControlFlowFlag.Next;
                
        public Variable ToConst()
        {
            Variable v = new Variable();            
            v.name = "";
            v.value = value.Copy();
            v.flag = flag;
            return v;
        }

        public Variable MakeLoaded(RuntimeVariableScope scope)
        {
            if (name.Length != 0)
            {
                // if (value == TypeNotNone.NotLoaded)
                {
                    value = scope.Load(name);
                }
            }
            return this;
        }

        public StatementVariable ToStatement()
        {
            return new StatementVariable(this);
        }
        public StatementConstVariable ToStatementConstVariable()
        {
            return new StatementConstVariable(this);
        }
        public Variable Copy()
        {
            Variable v = new Variable();
            v.name = name;
            v.value = value.Copy();
            v.flag = flag;
            return v;
        }

        public Variable WithFlag(ControlFlowFlag flag)
        {
            this.flag = flag;
            return this;
        }

        public bool HasControlFlowFlag()
        {
            return flag != ControlFlowFlag.Next;
        }

        public static Variable FromConstInt(Int64 value)
        {
            return new Variable() { value = new TypeInt(value) };
        }
        public static Variable FromConstDouble(double value)
        {
            return new Variable() { value = new TypeDouble(value) };
        }
        public static Variable FromConstString(string value)
        {
            return new Variable() { value = new TypeString(value) };
        }
        public static Variable FromConstBool(bool value)
        {
            return new Variable() { value = value ? TypeBool.trueValue : TypeBool.falseValue };
        }

        public static Variable FromName(string name)
        {
            return new Variable() {name=name, value = TypeNotNone.NotLoaded};
        }
    }

   

}
